package com.woniuxy.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.demo.Result;
import com.woniuxy.dto.AddAppraisalDto;
import com.woniuxy.dto.AppraisalHistoryDto;
import com.woniuxy.entity.AppraisalGoods;
import com.woniuxy.entity.CommodityTable;
import com.woniuxy.entity.GoodsSmall;
import com.woniuxy.exception.AppraisalException;
import com.woniuxy.param.AddAppraisalParam;
import com.woniuxy.service.AppraisalGoodsService;
import com.woniuxy.service.CommodityTableService;
import com.woniuxy.service.GoodsSmallService;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 鉴定记录 前端控制器
 * </p>
 *
 * @author 范腾龙
 * @since 2022-01-06
 * 商品鉴定相关接口
 */
@RestController
@RequestMapping("/appraisal-goods")
public class AppraisalGoodsController {

    @Autowired
    private AppraisalGoodsService appraisalGoodsService;

    @Autowired
    private GoodsSmallService goodsSmallService;

    @Autowired
    private CommodityTableService commodityTableService;

    /**
     * 根据商品id查找鉴定记录
     * @param commodityId
     * @return
     */
    @GetMapping("/appraisalHistoryInfo/{goodsSmallId}")
    @ApiOperation("根据商品id查找鉴定记录")
    @ResponseBody
    public Result appraisalHistoryInfo(@PathVariable("goodsSmallId") Integer commodityId){
        List<AppraisalGoods> appraisalHistoryList = appraisalGoodsService.list(new QueryWrapper<AppraisalGoods>().eq("commodity_table_id", commodityId));
        if (appraisalHistoryList.size()<=0) {
            return Result.newInstance().setMsg("该商品尚未鉴定！").setCode(500);
        }else {
            return Result.newInstance().setMsg("该商品鉴定记录查询成功！").setCode(200).setData(appraisalHistoryList);
        }
    }


    /**
     * 商品鉴定时根据商品信息中的小类id查找鉴定图信息
     * @param goodsSmallId
     * @return
     */
    @GetMapping("/appraisalPictureInfo/{goodsSmallId}")
    @ApiOperation("根据小类id查找鉴定图信息")
    @ResponseBody
    public Result appraisalPictureInfo(@PathVariable("goodsSmallId") Integer goodsSmallId){

        GoodsSmall goodsSmallDB = goodsSmallService.getById(goodsSmallId);
        AppraisalHistoryDto appraisalHistoryDto = new AppraisalHistoryDto();
        int count = appraisalGoodsService.count(new QueryWrapper<AppraisalGoods>().eq("commodity_table_id", goodsSmallId));
        if (goodsSmallDB!=null) {
            String appraisalPicture = goodsSmallDB.getGoodsSmallAppraisal();


            if (appraisalPicture.equals("")||appraisalPicture.isEmpty()) {
                throw new AppraisalException("该小类未设置鉴定图",499);
            }else {
                appraisalHistoryDto.setAppraisalGoodsPicture(appraisalPicture);

                appraisalHistoryDto.setAppraisalCount(count);
                return Result.newInstance().setCode(200).setMsg("鉴定图来了")
                        .setData(appraisalHistoryDto);
            }
        }
        throw new AppraisalException("小类信息不存在",500);
    }


    /**
     * 商品鉴定的后端接口
     * @param addAppraisalParam 前端传递的鉴定信息数据
     * @return
     */
    @PostMapping("/addAppraisalBycommodityId")
    @ApiOperation("商品鉴定的后端接口")
    @ResponseBody
    public Result addAppraisalBycommodityId(@RequestBody AddAppraisalParam addAppraisalParam){
        //先查询商品状态id 看看是否符合鉴定条件 只有goodsProcess为1时才可以鉴定
        CommodityTable commodityTable = commodityTableService.getById(addAppraisalParam.getCommodityTableId());
        Integer goodsProcess = commodityTable.getGoodsProcess();
        if (goodsProcess==2||goodsProcess==3||goodsProcess==4) throw new AppraisalException("不符合鉴定条件",1900);

        AppraisalGoods appraisalGoods = new AppraisalGoods();
        BeanUtils.copyProperties(addAppraisalParam,appraisalGoods);
        appraisalGoods.setAppraisalGoodsStatus(1);//修改鉴定状态 为1 表示已鉴定

        long now = System.currentTimeMillis();   //获取出来的是当前时间的毫秒值
        //把毫秒值转换成时间格式
        Date d=new Date();
        d.setTime(now);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        appraisalGoods.setAppraisalGoodsTime(format.format(d));//塞入鉴定时间
        appraisalGoodsService.save(appraisalGoods);
        AddAppraisalDto addAppraisalDto = new AddAppraisalDto();
        BeanUtils.copyProperties(appraisalGoods,addAppraisalDto);
        int count = appraisalGoodsService.count(new QueryWrapper<AppraisalGoods>().eq("commodity_table_id", addAppraisalParam.getCommodityTableId()));
        //塞入鉴定记录条数
        addAppraisalDto.setAppraisalCount(count);

        //改变商品信息的评鉴状态
        commodityTable.setGoodsProcess(2);//2表示已鉴定
        commodityTableService.updateById(commodityTable);//更新商品信息的数据库

        return Result.newInstance()
                .setData(addAppraisalDto)
                .setMsg("鉴定成功")
                .setCode(200);
    }
}

